<div id="addressMap" style="height: 430px;">
	
</div>
<script type="text/javascript">
	// window._customerLocation = "";
	var customerAddr = $('#addOrderForm').find('input[name="customerAddress"]').val();
	var customerLoca = $('#addOrderForm').find('input[name="customerLocation"]').val();
	if (window.customerAddress) {
		if (window.customerAddress.address == customerAddr) {
			mapInit(window.customerAddress.location);
		} else {
			if (customerAddr != "") {
				gaodeMapGeo(customerAddr, function(result) {
					mapInit(result.geocodes[0].location);
				});
			}
		}
	} else {
		if (customerAddr != "") {
			gaodeMapGeo(customerAddr, function(result) {
				mapInit(result.geocodes[0].location);
			});
		} else {
			mapInit();
		}
	}
	function mapInit (lastLocation) {
		var map = new AMap.Map('addressMap', {
	       zoom: 17,
	       center: lastLocation ? lastLocation.split(",") : undefined
	    });
	    if (lastLocation) {
	    	mapMarker(lastLocation.split(","), map);
	    }
     	AMap.plugin('AMap.ToolBar',function(){//异步加载插件
	        var toolbar = new AMap.ToolBar();
	        map.addControl(toolbar);
	    });
	    map.on('click', function(ev) {
	    	// 触发事件的地理坐标，AMap.LngLat 类型
  			var lnglat = ev.lnglat;
  			console.log(lnglat);
  			var position = [lnglat.P, lnglat.O];
  			window._customerLocation = position.join(",");
  			mapMarker(position, map);
	    });
	}
	
	function mapMarker(position, map) {
		clearMarker(map);
		// 构造点标记
		var marker = new AMap.Marker({
		    icon: "https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png",
		    position: position
		});
		map.add(marker);
	}
		
	function clearMarker(map) {
		map.clearMap();
	}
	// 高德地图地理编码
	function gaodeMapGeo(address, callback) {
		var ak = "83d2a8f1731ffd9706874206a8b5989f";
		var apiUrl = "https://restapi.amap.com/v3/geocode/geo?output=json&key=" + ak + "&address=" + address;
		var ajaxParms = {
			url: apiUrl,
			success: function(result) {
				callback(result);
			},
			error: function(err) {
				layer.msg('地址不正确，无法获取地址坐标', "系统提示");
			}
		}
		$.ajax(ajaxParms);
	}
</script>